/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;


import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import model.Usuario;
import view.frmManutencaoUsuario;

import model.Usuario;
import dao.UsuarioDAO;

/**
 *
 * @author francieli.selzler
 */
public class frmConsultaUsuario extends javax.swing.JFrame {

    /**
     * Creates new form frmConsultaUnidadeMedida
     */
    public frmConsultaUsuario() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelConsultaUnidadeMedida = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDescricaoUsuario = new javax.swing.JTextField();
        btnPesquisarUsuario = new javax.swing.JButton();
        jPanelConsultaUnidadeMedida1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblConsultaUsuarios = new javax.swing.JTable();
        btnIncluirUsuario = new javax.swing.JButton();
        BtnStatus = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Usuários");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jPanelConsultaUnidadeMedida.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros"));
        jPanelConsultaUnidadeMedida.setToolTipText("");
        jPanelConsultaUnidadeMedida.setName("jPanelConsultaUnidadeMedida"); // NOI18N

        jLabel1.setText("Nome:");

        btnPesquisarUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/search.png"))); // NOI18N
        btnPesquisarUsuario.setText("Pesquisar");

        javax.swing.GroupLayout jPanelConsultaUnidadeMedidaLayout = new javax.swing.GroupLayout(jPanelConsultaUnidadeMedida);
        jPanelConsultaUnidadeMedida.setLayout(jPanelConsultaUnidadeMedidaLayout);
        jPanelConsultaUnidadeMedidaLayout.setHorizontalGroup(
            jPanelConsultaUnidadeMedidaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConsultaUnidadeMedidaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelConsultaUnidadeMedidaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnPesquisarUsuario)
                    .addGroup(jPanelConsultaUnidadeMedidaLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtDescricaoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(237, 237, 237))
        );
        jPanelConsultaUnidadeMedidaLayout.setVerticalGroup(
            jPanelConsultaUnidadeMedidaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConsultaUnidadeMedidaLayout.createSequentialGroup()
                .addGroup(jPanelConsultaUnidadeMedidaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtDescricaoUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(btnPesquisarUsuario)
                .addContainerGap())
        );

        jPanelConsultaUnidadeMedida1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        jPanelConsultaUnidadeMedida1.setToolTipText("");
        jPanelConsultaUnidadeMedida1.setName("jPanelConsultaUnidadeMedida"); // NOI18N

        tblConsultaUsuarios.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Código", "Nome", "Status"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblConsultaUsuarios.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConsultaUsuariosMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblConsultaUsuarios);

        btnIncluirUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/new.png"))); // NOI18N
        btnIncluirUsuario.setText("Incluir");
        btnIncluirUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnIncluirUsuarioActionPerformed(evt);
            }
        });

        BtnStatus.setText("Ativar");
        BtnStatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnStatusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelConsultaUnidadeMedida1Layout = new javax.swing.GroupLayout(jPanelConsultaUnidadeMedida1);
        jPanelConsultaUnidadeMedida1.setLayout(jPanelConsultaUnidadeMedida1Layout);
        jPanelConsultaUnidadeMedida1Layout.setHorizontalGroup(
            jPanelConsultaUnidadeMedida1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConsultaUnidadeMedida1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelConsultaUnidadeMedida1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 974, Short.MAX_VALUE)
                    .addGroup(jPanelConsultaUnidadeMedida1Layout.createSequentialGroup()
                        .addComponent(btnIncluirUsuario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BtnStatus)))
                .addContainerGap())
        );
        jPanelConsultaUnidadeMedida1Layout.setVerticalGroup(
            jPanelConsultaUnidadeMedida1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelConsultaUnidadeMedida1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelConsultaUnidadeMedida1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnIncluirUsuario)
                    .addComponent(BtnStatus))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/rodape_sisgp.png"))); // NOI18N
        jLabel3.setAlignmentY(0.0F);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelConsultaUnidadeMedida, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelConsultaUnidadeMedida1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelConsultaUnidadeMedida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelConsultaUnidadeMedida1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel3))
        );

        jPanelConsultaUnidadeMedida.getAccessibleContext().setAccessibleName("Nome Acesível");
        jPanelConsultaUnidadeMedida.getAccessibleContext().setAccessibleDescription("Descrição Acessível");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnIncluirUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnIncluirUsuarioActionPerformed
       
        frmManutencaoUsuario frmManutencao = new frmManutencaoUsuario();
        frmManutencao.setVisible(true);
    }//GEN-LAST:event_btnIncluirUsuarioActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        // TODO add your handling code here:
        listaDados();
        
    }//GEN-LAST:event_formWindowActivated

    private void tblConsultaUsuariosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConsultaUsuariosMouseClicked
        // TODO add your handling code here:
        
        BtnStatus.setEnabled(false);
        
        status = null;
        
        login = ""+tblConsultaUsuarios.getValueAt(tblConsultaUsuarios.getSelectedRow(), 0);
        
        status = ""+tblConsultaUsuarios.getValueAt(tblConsultaUsuarios.getSelectedRow(), 2);
        
        if(status.trim().equals("Ativo") || status.trim().equals("Inativo"))
        {
              
            BtnStatus.setEnabled(true);
            
            if(status.trim().equals("Ativo"))
            {
                
                BtnStatus.setText("Desativar");
                
            }
            else
            {
                
                BtnStatus.setText("Ativar");
                
            }    
 
        }
        else
        {
            
            BtnStatus.setEnabled(false);
            
        }
        
    }//GEN-LAST:event_tblConsultaUsuariosMouseClicked

    private void BtnStatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnStatusActionPerformed
        
        if(BtnStatus.getText().trim().equals("Ativar"))
        {
            
            //codigo de ativação
            
        }
        
        if(BtnStatus.getText().trim().equals("Inativar"))
        {
            //codigo de desativação
        }
        
    }//GEN-LAST:event_BtnStatusActionPerformed

    /**
     * @param args the command line arguments
     */
    
    
    String login;
    String status;
    UsuarioDAO userDAO = new UsuarioDAO();
    
    
    private void listaDados(){
    
        BtnStatus.setEnabled(false);
        
        DefaultTableModel dados = (DefaultTableModel) tblConsultaUsuarios.getModel();
        
        dados.setNumRows(0);
        
        for (Usuario user : userDAO.findAll())
        {
            String gstatus;
            
            if(user.getStatus() == 1)
            {
                 gstatus = "Ativo";
            }
            else
            {
                gstatus = "Inativo";
            }
            
            dados.addRow(new String[]{""+user.getLogin(), ""+user.getNome(), ""+gstatus});
        }
        
    }
   
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnStatus;
    private javax.swing.JButton btnIncluirUsuario;
    private javax.swing.JButton btnPesquisarUsuario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelConsultaUnidadeMedida;
    private javax.swing.JPanel jPanelConsultaUnidadeMedida1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblConsultaUsuarios;
    private javax.swing.JTextField txtDescricaoUsuario;
    // End of variables declaration//GEN-END:variables
}
